﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;

namespace Net.DataAccess
{
	public abstract class TypeProvider : DataAccess
	{
		static private TypeProvider _instance = null;
		static public TypeProvider Instance
		{
			get
			{
				if (_instance == null)
					_instance = (TypeProvider)Activator.CreateInstance(
					   Type.GetType("Net.SqlProvider.CategoryTypeSP"));
				return _instance;
			}
		}

		public TypeProvider()
		{
			this.ConnectionString = Globals.Settings.ConnectionString;
			this.EnableCaching = Globals.Settings.EnableCaching;
			this.CacheDuration = Globals.Settings.CacheDuration;
		}

		public abstract TypeDetails FindByID(string id);
		public abstract string Insert(TypeDetails obj);

		protected virtual TypeDetails GetFromReader(IDataReader reader)
		{
			return new TypeDetails(
				reader["ID"].ToString(),
				(DateTime)reader["AddedDate"],
				reader["AddedBy"].ToString(),
				reader["Name"].ToString(),
				reader["Description"].ToString());
		}

		protected virtual List<TypeDetails> GetListFromReader(IDataReader reader)
		{
			List<TypeDetails> items = new List<TypeDetails>();
			while (reader.Read())
				items.Add(GetFromReader(reader));
			return items;
		}
	}
}